package com.xsh.web.weixin;


import com.xsh.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import weixin.popular.api.MessageAPI;
import weixin.popular.api.QrcodeAPI;
import weixin.popular.bean.message.templatemessage.TemplateMessage;
import weixin.popular.bean.message.templatemessage.TemplateMessageItem;
import weixin.popular.bean.message.templatemessage.TemplateMessageResult;
import weixin.popular.bean.qrcode.QrcodeTicket;
import weixin.popular.support.TokenManager;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author lixing
 * @date 2021/5/25 15:47
 */
@Slf4j
@Controller
@RequestMapping("/msg")
public class WxMsgController {


    @Autowired
    TokenUtil tokenUtil;

    /**
     * 发送模板消息
     * @param param
     * @return
     */
    @RequestMapping(value = "")
    @ResponseBody
    public TemplateMessageResult list(@RequestParam Map<String, String> param){
        TemplateMessage templateMessage = new TemplateMessage();
        templateMessage.setUrl("http://www.baidu.com");
//        templateMessage.setTouser((String) request.getSession().getAttribute("openId"));
        String openId = param.get("openId");
        if (StringUtils.isEmpty(openId)){
            openId = "o7lHm5yNPtqNhkggG32P8qNUZGnY";
        }
        templateMessage.setTouser(openId);//openid
        templateMessage.setTemplate_id("NGTht2Q7ZksxTx4ntDQvKnPEstRUFprg86Z7szV6HyI");//模板id
        //模板参数赋值
        LinkedHashMap<String, TemplateMessageItem> hashMap = new LinkedHashMap<>();
        TemplateMessageItem first = new TemplateMessageItem();
        /**模板内容
         * {{first.DATA}} 订单编号：{{keyword1.DATA}} 订单类型：{{keyword2.DATA}} 商品名称：{{keyword3.DATA}} {{remark.DATA}}
         */
        first.setColor("#000000");
        first.setValue("您好，您有一条待确认订单。");
        hashMap.put("first",first);

        TemplateMessageItem keyword1  = new TemplateMessageItem();
        keyword1 .setColor("#328392");
        keyword1 .setValue("预定订单");
        hashMap.put("keyword1",keyword1 );

        TemplateMessageItem keyword2 = new TemplateMessageItem();
        keyword2.setColor("#328392");
        keyword2.setValue("可口可乐");
        hashMap.put("keyword2",keyword2);

        TemplateMessageItem keyword3 = new TemplateMessageItem();
        keyword3.setColor("#929232");
        keyword3.setValue("请及时确认订单！");
        hashMap.put("keyword3",keyword3);

        templateMessage.setData(hashMap);
        String token = "45_q9fcKGFzJW8JF3oiBZP9GD9o1xqRqU8QAP_3MYjC34dK-7QC0YfBRQFXJ6JT-HaK-lq63o1JzIXwLrzrkI0jyiJSt4ED0BMGyqLGh1Z4N0g3fElanzx_gIpVU21C37CID3g9qmaXq12F4ge5JHVeAHAAYM";
        //token两个小时失效 应当放到缓存 测试号每天只能获取2000次
        String access_token = tokenUtil.getAccess_token();
        TemplateMessageResult messageTemplateSend = MessageAPI.messageTemplateSend(access_token, templateMessage);
        return messageTemplateSend;
    }

    /**
     * 推广二维码
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("/ms")
    public QrcodeTicket m(HttpServletRequest request, HttpServletResponse response) throws IOException {
        QrcodeTicket qrcodeTicket = QrcodeAPI.qrcodeCreateTemp(TokenManager.getDefaultToken(), 3600, "123456");
        BufferedImage showqrcode = QrcodeAPI.showqrcode(qrcodeTicket.getTicket());

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(showqrcode,"png",outputStream);
        byte b[] = outputStream.toByteArray();

        response.getOutputStream().write(b);
        return qrcodeTicket;
    }
}
